﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.Artwork;
using Newtonsoft.Json;

namespace WebApplication.Art
{
    public partial class Detail : System.Web.UI.Page
    {
        private ArtworkService artworkService = new ArtworkService();

        public Models.Artworks artwork = null;

        public List<string> tags = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(Request.QueryString["id"] == null)
            {
                Response.Redirect("/");
                return;
            }

            int artworkId = Convert.ToInt32(Request.QueryString["id"]);


            artwork = artworkService.GetArtworkById(artworkId);
            if (Session["UID"] != null)
            {
                int uid = Convert.ToInt32(Session["UID"]);
                artworkService.QueryIsLiked(uid, artwork);
            } 

            tags = JsonConvert.DeserializeObject<List<string>>(artwork.Tag);
            if(tags.Count > 0)
            {
                tags.Remove("@");
            }

            try
            {
                artworkService.IncrementHot(artwork);
                artwork.Pics = JsonConvert.DeserializeObject<List<string>>(artwork.Items);
            } catch (NullReferenceException)
            {
                Response.Redirect("/");
            }
            
        }
    }
}